﻿
namespace QFramework
{
    using UnityEngine;
    using FairyGUI;

    public class FGUIBehaviour : QMonoBehaviour,IUIBehaviour
    {
        public GComponent m_Compoent;

        protected override void SetupMgr()
        {
            mCurMgr = FGUIManager.Instance;
        }

        public Transform Transform { get { return transform; } }

        public void Init(IUIData uiData = null)
        {
            InnerInit(uiData);
            RegisterUIEvent();
        }

        void InnerInit(IUIData uiData = null)
        {
            InitUI(uiData);
            OnShow();
        }

        protected virtual void InitUI(IUIData uiData = null)
        {

        }

        protected virtual void RegisterUIEvent()
        {

        }

        void IUIBehaviour.Close(bool destroyed = true)
        {
             
        }

        protected sealed override void OnDestroy()
        {
            base.OnDestroy();
        }

        protected virtual void GetAllButton()
        {
            if (m_Compoent != null)
            {
                int cnt = m_Compoent.numChildren;
                for (int i = 0; i < cnt; i++)
                {
                    GObject obj = m_Compoent.GetChildAt(i);
                    if (obj.name.StartsWith("btn_"))
                        obj.onClick.Add(ButtonCallBack);
                }
            }
        }

        private void ButtonCallBack(EventContext context)
        {
            string type = ((GObject)(context.sender)).name.Substring(4);
            Button(type);
        }

        protected virtual void Button(string ButtonName)
        {

        }

        protected virtual void OnClose()
        {
        }

        public virtual void Dispose()
        {
            if (m_Compoent != null)
                m_Compoent.Dispose();
        }

        private System.Action mOnPanelClosed;

        public void OnClosed(System.Action onPanelClosed)
        {
            mOnPanelClosed = onPanelClosed;
        }

    }
}
